package com.yanchangapp.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.IBinder;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
import com.yanchangapp.R;
import com.zxl.baselib.util.ui.UIUtils;

/**
 * Created by apple on 2019-09-06.
 * description: 商品数量的dialog
 * 看来dialog确实有bug啊
 *
 * (也就是说分为最大库存和最小库存之说)
 */
public class GoodsNumDialog extends Dialog {

    public GoodsNumDialog(@NonNull Context context) {
        super(context);
    }

    public GoodsNumDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public interface GoodsEnsureClickListener{

        void onEnsureClickSuccess(int amount);
    }

    @Override
    public void dismiss() {
        InputMethodManager imm = (InputMethodManager) getContext().getApplicationContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        // 即使当前焦点不在editText，也是可以隐藏的。
        if (imm != null) {
            final View currentFocus = getCurrentFocus();
            IBinder windowToken = null;
            if (currentFocus != null) {
                windowToken = currentFocus.getWindowToken();
            }
            if (windowToken != null) {
                imm.hideSoftInputFromWindow(windowToken, 0);
            }
        }
        super.dismiss();
    }

    public static class Builder{
        private Context mContext;
        private int mGoodsStorage;
        private int number;
        private GoodsEnsureClickListener mGoodsEnsureClickListener;

        public Builder(Context context) {
            mContext = context;
        }

        public Builder setGoodsStorage(int num){
            this.mGoodsStorage = num;
            return this;
        }

        public Builder setCurrentNum(int number){
            this.number = number;
            return this;
        }

        public Builder setGoodsEnsureListener(GoodsEnsureClickListener listener){
            this.mGoodsEnsureClickListener = listener;
            return this;
        }

        public GoodsNumDialog build(){
            final GoodsNumDialog goodsNumDialog = new GoodsNumDialog(mContext, R.style.MyDialog);
            View localView =  LayoutInflater.from(mContext).inflate(R.layout.dialog_set_num,null);
            TextView mTvCancel = localView.findViewById(R.id.tvCancel);
            TextView mTvEnsure = localView.findViewById(R.id.tvEnsure);
            EditText etNum = localView.findViewById(R.id.etNum);
            etNum.setText(String.valueOf(number));
            etNum.setSelection(String.valueOf(number).length());
            TextView tvCurrentNumDes = localView.findViewById(R.id.tvCurrentNum);
            if (mGoodsStorage != Integer.MAX_VALUE){
                tvCurrentNumDes.setText("最大库存:"+mGoodsStorage);
            }

            // 取消,那么直接关闭即可
            mTvCancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goodsNumDialog.dismiss();
                }
            });

            mTvEnsure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String strNum = etNum.getText().toString();
                    if (strNum.length() > 9){
                        UIUtils.showToast("数量不能超过999999999");
                        return;
                    }
                    int amount = Integer.parseInt(strNum);
                    if (amount > mGoodsStorage){
                        tvCurrentNumDes.setVisibility(View.VISIBLE);
                        return;
                    }

                    if (mGoodsEnsureClickListener != null){
                        mGoodsEnsureClickListener.onEnsureClickSuccess(amount);
                    }
                    goodsNumDialog.dismiss();

                }
            });
            goodsNumDialog.setContentView(localView);
            return goodsNumDialog;
        }
    }


}
